package com.huawei.ivs.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.ivs.v2.IvsClient;
import com.huaweicloud.sdk.ivs.v2.model.DetectExtentionByNameAndIdRequest;
import com.huaweicloud.sdk.ivs.v2.model.DetectExtentionByNameAndIdResponse;
import com.huaweicloud.sdk.ivs.v2.model.ExtentionReqDataByNameAndId;
import com.huaweicloud.sdk.ivs.v2.model.IvsExtentionByNameAndIdRequestBody;
import com.huaweicloud.sdk.ivs.v2.model.IvsExtentionByNameAndIdRequestBodyData;
import com.huaweicloud.sdk.ivs.v2.model.Meta;
import com.huaweicloud.sdk.ivs.v2.region.IvsRegion;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class Demo {

    private static final Logger LOGGER = LoggerFactory.getLogger(Demo.class.getName());

    // 认证人姓名
    private static final String VERIFICATION_ID = "张三";

    // 认证人身份证号码
    private static final String VERIFICATION_NAME = "612************11";

    public static void main(String[] args) {
        // 替换为真实的AK SK
        String ak = "Qa2**************QdV";
        String sk = "aBD**********************************cRv";

        // Init Auth Info
        ICredential credential = getCredential(ak, sk);

        // create IvsClient
        IvsClient client = getClient(IvsRegion.CN_NORTH_4, credential);

        // 二要素姓名身份证认证
        detectExtentionByNameAndId(client);

    }

    /**
     * 二要素姓名身份证认证
     *
     * @param client 初始化的ivs客户端
     */
    private static void detectExtentionByNameAndId(IvsClient client) {
        DetectExtentionByNameAndIdRequest byNameAndIdRequest = new DetectExtentionByNameAndIdRequest();
        IvsExtentionByNameAndIdRequestBody requestBody = new IvsExtentionByNameAndIdRequestBody();
        IvsExtentionByNameAndIdRequestBodyData requestBodyData = new IvsExtentionByNameAndIdRequestBodyData();
        List<ExtentionReqDataByNameAndId> extentionReqDataByNameAndIds = new ArrayList<>();
        ExtentionReqDataByNameAndId byNameAndId = new ExtentionReqDataByNameAndId();
        byNameAndId.setVerificationId(VERIFICATION_ID);
        byNameAndId.setVerificationName(VERIFICATION_NAME);

        extentionReqDataByNameAndIds.add(byNameAndId);
        requestBodyData.withReqData(extentionReqDataByNameAndIds);
        requestBody.withData(requestBodyData);

        Meta meta = new Meta();
        meta.setUuid("uuid");
        requestBody.withMeta(meta);
        byNameAndIdRequest.setBody(requestBody);
        try {
            DetectExtentionByNameAndIdResponse response = client.detectExtentionByNameAndId(byNameAndIdRequest);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            LOGGER.error(e.toString());
        } catch (RequestTimeoutException e) {
            LOGGER.error(e.toString());
        } catch (ServiceResponseException e) {
            LOGGER.error(e.getErrorMsg());
        }
    }

    /**
     * 获取鉴权
     *
     * @param ak 开通人证核身服务账号的AK
     * @param sk 开通人证核身服务账号的SK
     * @return 认证信息
     */
    public static ICredential getCredential(String ak, String sk) {
        return new BasicCredentials().withAk(ak).withSk(sk);
    }

    /**
     * 初始化客户端
     *
     * @param region 开通服务的RegionID
     * @param auth 认证信息
     * @return ivs客户端
     */
    public static IvsClient getClient(Region region, ICredential auth) {
        // 跳过SSL认证，可删除
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);
        return IvsClient.newBuilder().withCredential(auth).withRegion(region).withHttpConfig(config).build();
    }

}
